window._MJG = {

    _CURRENTLABEL:null,

    npcForJg:null,

    getNpcForJg:function(){
        if(_MJG.npcForJg==null){
        _MJG.npcForJg = [];
            for(var key in _NPC){

                var npcconfig = _NPC[key];
                if(!npcconfig.isboss
                    &&_GAMEDATAHELPER.hasNpcTj(key)
                ){
                    _MJG.npcForJg.push(key);
                }
            }
        }
        return _MJG.npcForJg;
    },

    showJG:function(parentNode){

        //_TIPS.showMessage("酒馆暂未开放");
        //return ;

        var uiObj = _GRM.getNodeObj('jiuguan1');
        var mainLabel = uiObj.node;

        _MJG._CURRENTLABEL = mainLabel;

        _MJG.npcForJg = null;
        
        if(uiObj.isnew){
            _MJG.initButton(mainLabel);
        }

       
        _MJG.refreshLabel();

        //_MJG.refreshDesc();

        parentNode.addChild(mainLabel);
    },

    initButton:function(mainLabel){

        mainLabel.getChildByName('mask').on(cc.Node.EventType.TOUCH_START, function(){
            _GRM.putNode(this);
        }, mainLabel);

        mainLabel.getChildByName('sxdj').on(cc.Node.EventType.TOUCH_START, function(){
            if(_GAMEDATAHELPER.oprMoney(-10000)){
                _MJG.refreshJg();
            }else{
                _TIPS.showMessage('金不足(10000金刷新一次)');
            }
        }, mainLabel);

        mainLabel.getChildByName('smdj').on(cc.Node.EventType.TOUCH_START, function(){
            //_Ep.showMessage(_SHOPHELPER.showShopLevelDesc('酒馆商'));
            var randomJobArr = _MXLU.getObjArrOfXw('酒馆商');   
            if(randomJobArr&&randomJobArr.length>0){
                var message = "当前酒馆刷新职业:\n"+randomJobArr.join(',');
                _Ep.showMessage(message);
            }else{
                _Ep.showMessage('暂无');
            }
        }, mainLabel);

        mainLabel.getChildByName('sjdj').on(cc.Node.EventType.TOUCH_START, function(){
            if(!_MJG.ismax()){
            
                if(_GAMEDATAHELPER.upShopLevel('酒馆商')){
                    _MJG.refreshDesc();
                }else{
                    _TIPS.showMessage('木头不足升级失败');
                }
            }else{
                _TIPS.showMessage('酒馆已满级');
            }

        }, mainLabel);

        var goodsArr = mainLabel.getChildByName('background').getChildByName('goods').getChildren();
        for(var i=0;i<goodsArr.length;i++){

            var goods = goodsArr[i].getChildByName('buttonUp');
            goods.goodsIndex = i;
            goods.on(cc.Node.EventType.TOUCH_START, function(){
                //console.log('招募:',this.goodsIndex);
                //_MJG.refreshJg();
                var role = window.gameData.jgArr[this.goodsIndex];
                var price = _MJG.getPriceOfRole(role);
                if(_GAMEDATAHELPER.oprMoney(-price)){
                    window.gameData.playerTeamBack.unshift(role);
                    window.gameData.jgArr.splice(this.goodsIndex,1);
                    _MJG.refreshLabel();
                    if(role.job&&_JOB[role.job]){
                        _TTCH.doCompleteTask('getRole',_JOB[role.job].level,true);
                    }
                }else{
                    _TIPS.showMessage('金钱不足无法雇佣');
                }
             }, goods);


        }
    },

    ismax:function(){

        var max = _SHOP['酒馆商'].sell.length;
        var currentShopLevel = _GAMEDATAHELPER.getShopLevel('酒馆商');

        if(currentShopLevel<max-1){
            return false;
        }

        return true;

    },


    refreshDesc:function(){

        var mainLabel = _MJG._CURRENTLABEL;
        var descNode = mainLabel.getChildByName('desc');
        var currentShopLevel = _GAMEDATAHELPER.getShopLevel('酒馆商')+1;
        if(!_MJG.ismax()){
            descNode.getComponent(cc.Label).string =  "酒馆商等级:"+currentShopLevel
            +"\n升级所需:"+_GAMEDATAHELPER.getWood()+"/"+currentShopLevel;
        }else{
            descNode.getComponent(cc.Label).string =  "酒馆商等级:"+currentShopLevel
            +"\n已满级";
        }
    },


    refreshLabel:function(){
        var mainLabel = _MJG._CURRENTLABEL;
        var roleArr = _MJG.getJgRole();
        var goodsArr = mainLabel.getChildByName('background').getChildByName('goods').getChildren();
        for(var i=0;i<goodsArr.length;i++){

            var goods = goodsArr[i];
            if(roleArr[i]){
                goods.active = true;
                _MJG.refreshOne(goods,roleArr[i]);
            }else{
                goods.active = false;
            }


        }

    },

    getPriceOfRole:function(role){

        return 100000;
    },

    refreshOne:function(goods,role){

        var npcconfig = _NPC[role.npctype];
        var faceNode = goods.getChildByName('pos').getChildByName('touxian');
        var infoNode = goods.getChildByName('info');
        var ftypeNode = goods.getChildByName('pos').getChildByName('wuxin');
        var jobNode = goods.getChildByName('pos').getChildByName('job');
        var zhongNode = goods.getChildByName('pos').getChildByName('zhong');
        var levelNode = goods.getChildByName('pos').getChildByName('level');


        faceNode.getComponent(cc.Sprite).spriteFrame = _GRM.getFaceFrame(role.face);
        ftypeNode.color = new cc.color(_CS.ftype[role.ftype].color);
        ftypeNode.getChildByName('label')
        .getComponent(cc.Label).string = role.ftype;

        jobNode.color = new cc.color(_JOB[npcconfig.job].color);
        jobNode.getChildByName('label')
        .getComponent(cc.Label).string = _JOB[npcconfig.job].sname;

        zhongNode.color = new cc.color(_RACE[npcconfig.race].color);
        zhongNode.getChildByName('label')
        .getComponent(cc.Label).string = _RACE[npcconfig.race].name;

        levelNode.color = new cc.color(_CS.qua[npcconfig.level].color);
        levelNode.getChildByName('label')
        .getComponent(cc.Label).string = npcconfig.level;


        infoNode.getComponent(cc.Label).string = role.name+" LV.1"+
                                        "\n"+role.npctype+
                                        "\n武"+role.plv+" 智"+role.ilv+" 体"+role.hlv+
                                        "\n统"+role.dlv+" 德"+role.mlv+
                                        "\n价格:"+_MJG.getPriceOfRole(role);
        infoNode.color = new cc.color(_MJG.getJobColor(role.job));
    },

    getJobColor:function(job){

        if(_JOB[job]&&(
            _JOB[job].level
            ||_JOB[job].level==0
        )){

            var qua = "C";
            if(_JOB[job].level==0){
                qua='D';
            }else if(_JOB[job].level==1){
                qua='C';
            }else if(_JOB[job].level==2){
                qua='B';
            }else if(_JOB[job].level==3){
                qua='A';
            }else if(_JOB[job].level==4){
                qua='S';
            }else if(_JOB[job].level==5){
                qua='SS';
            }

            return _CS.qua[qua].color;
        }

        return "#ffffff";
    },

    refreshJg:function(){

        window.gameData.jgArr=[];
        //_MJG.randomJg();
        _MJG.refreshLabel();

    },

    getJgRole:function(){

        _MJG.randomJg();
        return window.gameData.jgArr;
    },

    randomJg:function(){

        if(!window.gameData.jgArr){
            window.gameData.jgArr=[];
        }      

        if(window.gameData.jgArr.length<=0){

            for(var i=0;i<6;i++){
         
                var tmpnpc = _CROF.getRandomMainRole(false,null,null);
                
                if(tmpnpc)
                window.gameData.jgArr.push(tmpnpc);

            }

        }

    }




}